import { createSlice } from "@reduxjs/toolkit";
import { getInfo } from "@/api/adminApi";

const userSlice = createSlice({
  name: "user",
  initialState: {
    userInfo: {},
  },
  reducers: {
    setUserInfo: (state, userInfo) => {
      state.userInfo = userInfo;
    },
  },
});

const { setUserInfo } = userSlice.actions;

const GetUserInfo = () => {
  return new Promise((resolve, reject) => {
    getInfo()
      .then((response) => {
        const { user } = response.data;
        setUserInfo(user)
        resolve(response);
      })
      .catch((error) => {
        reject(error);
      });
  });
};

export { GetUserInfo };
export default userSlice.reducer;
